package com.hxzy.controller.mobile.address;

import com.hxzy.common.annotates.StpMobileCheckLogin;
import com.hxzy.common.aspect.satoken.StpMobileUtil;
import com.hxzy.common.controller.BaseController;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.resp.R;
import com.hxzy.controller.mobile.address.req.AddressBookAddReq;
import com.hxzy.controller.mobile.address.req.AddressBookEditReq;
import com.hxzy.controller.mobile.address.resp.AddressBookResp;
import com.hxzy.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/mobile/addressBook")
@Api(tags = "手机-用户地址API")
@StpMobileCheckLogin
public class MobileAddressController extends BaseController {

    @Autowired
    private AddressBookService addressBookService;


    @ApiOperation(value = "获取默认收货地址")
    @GetMapping(value = "/default")
    public R<AddressBookResp> defaultAddressInfo(){
        AddressBookResp  resp=this.addressBookService.defaultAddressInfo();
        return  R.okHasData(resp);
    }

    @ApiOperation(value = "当前用户所有收货地址列表")
    @GetMapping(value = "/data")
    public R<List<AddressBookResp>> search(){
        List<AddressBookResp> list=this.addressBookService.search();
        return  R.okHasData(list);
    }

    @ApiOperation(value = "修改")
    @PutMapping
    public R editData(@RequestBody @Valid AddressBookEditReq req){
        boolean result=this.addressBookService.editData(req);
        return result?R.ok(): R.build(AckCode.DATABASE_ERROR);
    }

    @ApiOperation(value = "新增")
    @PostMapping
    public R addData(@RequestBody @Valid AddressBookAddReq req){
        boolean result=this.addressBookService.addData(req);
        return result?R.ok(): R.build(AckCode.DATABASE_ERROR);
    }

    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/{id}")
    public R<AddressBookResp> findById(@PathVariable(value = "id") Long id){
        long userId = StpMobileUtil.getLoginIdAsLong();
        AddressBookResp resp= this.addressBookService.findById(id,userId);
        return R.okHasData(resp);
    }

    @ApiOperation(value = "设定默认收货地址")
    @GetMapping(value = "/default/{addressId}")
    public R defaultAddress(@PathVariable(value = "addressId") Long id){
        boolean result=this.addressBookService.defaultAddress(id);
        return result?R.ok(): R.build(AckCode.DATABASE_ERROR);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/{id}")
    public R remove( @PathVariable(value = "id") long id){
        boolean result=this.addressBookService.delete(id);
        return result?R.ok(): R.build(AckCode.DATABASE_ERROR);
    }
}




